package com.ham.mud.commands;

import com.ham.mud.ServerConnection;
import com.ham.mud.TargetableService;
import com.ham.mud.characters.player.Player;
import com.ham.mud.characters.player.PlayerService;
import com.ham.mud.items.Item;
import com.ham.mud.rooms.Room;
import com.ham.mud.rooms.RoomService;

import java.util.List;

/**
 * Created by hlucas on Jun 21, 2011 at 2:57:08 PM
 */
public class DropCommand extends PlayerCommand {
    @Override
    public void execute(ServerConnection connection, String[] params) {
        if(params.length == 0) {
            connection.printSolo("Drop what?");
            return;
        }

        String search = params[0];

        Player player = connection.getPlayer();
        int atX = player.getAtX();
        int atY = player.getAtY();

        Room room = RoomService.getRoom(player.getZone(), atX, atY);
        List<Item> items = player.getInventory();

        Item item = (Item) TargetableService.findTarget(search, items);
        if(item == null) {
            connection.printSolo("You don't have one of those.");
            return;
        }

        player.removeItem(item);
        room.addItem(item);

        connection.printSolo("You drop " + item.getName() + ".");
        room.printToOthers(player.getName() + " drops " + item.getName() + ".", connection);
        PlayerService.save(player);
    }

    @Override
    protected String getCommandName() {
        return "drop";
    }
}
